644A - Parliament of Berland - CodeForces Solution


*special problem constructive algorithms *1000

Please click on ads to support us..

Python Code:

import math
from collections import deque


alfabet = {'a': 1, 'b': 2,'c': 3,'d': 4,'e': 5,'f': 6,'g': 7,'h': 8,'i': 9,'j': 10,'k': 11,'l': 12,'m': 13,'n': 14,'o': 15,'p': 16,'q': 17,'r': 18,'s': 19,'t': 20,'u': 21,'v': 22,'w': 23,'x': 24,'y': 25,'z': 26}
rasp=''
for i in range(0,1):
 n,a,b=map(int,input().split())
    
    cd=1
 cr=2
 ct=0
 
 if n>a*b:
  print(-1)
 else: 
  for i in range(0,a):
   for j in range(0,b):
    if (i&1)==(j&1):
     if cd<=n:
      print(cd,end=' ')
      cd+=2
     else:
      print(0, end=' ')
    else:
      if cr<=n:
       print(cr,end=' ')
       cr+=2
      else:
       print(0, end=' ')
   print()
 
 

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){
ll n,a,b; cin>>n>>a>>b;
if(a*b<n){
    cout<<"-1"; return 0;
}
ll s[a][b]={0};
for(ll i=0;i<a;i++){
for(ll j=0;j<b;j++){
    s[i][j]=0;
}
}
ll i=0,j=0,k=0,r=1;
while(1){
    s[i][j]=r;
    if(r==n) break;
    else r++;

    if(k==0) j++;
    if(k==1) j--;
    if(j==b){
        j--; i++; k=1;
    }
    if(j==-1){
        j++; i++; k=0;
    }
}
for(ll i=0;i<a;i++){
for(ll j=0;j<b;j++){
    cout<<s[i][j]<<" ";
}
cout<<endl;
}
}


Comments

Submit
0 Comments
More Questions

Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements